package com.jming.helloworld.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.jming.common.easycaptcha.LoginService;

@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @PostMapping
    public String login(@RequestParam String username, @RequestParam String password, @RequestParam String captchaCode) {
        if (!loginService.verifyCaptcha(captchaCode)) {
            // 验证码错误
            return "redirect:/login?error=captcha";
        }

        // 校验用户名和密码
        if (username.equals("admin") && password.equals("123456")) {
            // 登录成功
            return "redirect:/home";
        }

        // 登录失败
        return "redirect:/login?error=login";
    }

}